package com.yuwei.user

import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.tencent.mmkv.MMKV
import com.yuwei.base.RoutePath
import com.yuwei.common.mvi.BaseViewActivity
import com.yuwei.user.databinding.ActivityLoginBinding
import com.yuwei.user.entity.GetPermissions

/**
 * 输入框 校验
 * 1.长度限制
 * 2.字符内容限制(大小写字母，下划线，数字)
 * 3.字符内容 涉政涉黄
 * 4.特殊的(手机号，邮箱，身份证)
 */
@Route(path = RoutePath.LOGIN_ACTIVITY)
class LoginActivity : BaseViewActivity<ActivityLoginBinding>() {
    override fun initView() {
        //跳转至帮助页面
        binding.tvHelp.setOnClickListener {
            ARouter.getInstance().build(RoutePath.MODULE_LOGIN_HELP_ACTIVITY).navigation()
        }
        //富文本
        val spannable = SpannableStringBuilder(binding.tvPrivacy.text.toString())
        val spannable1 = SpannableStringBuilder(binding.tvProtocol.text.toString())
        val spannable2 = SpannableStringBuilder(binding.tvLogin.text.toString())
        spannable.setSpan(object :ClickableSpan(){
            override fun onClick(p0: View) {
                ARouter.getInstance().build(RoutePath.MODULE_LOGIN_WEB_ACTIVITY)
                    .withString("url","https://terms.alicdn.com/legal-agreement/terms/suit_bu1_unification/suit_bu1_unification202005142208_14749.html")
                    .navigation()
            }
        },0,4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        spannable.setSpan(ForegroundColorSpan(Color.parseColor("#807F7F")), 0, 4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        binding.tvPrivacy.setText(spannable)
        binding.tvPrivacy.setMovementMethod(LinkMovementMethod.getInstance())
        spannable1.setSpan(object :ClickableSpan(){
            override fun onClick(p0: View) {
                ARouter.getInstance().build(RoutePath.MODULE_LOGIN_WEB_ACTIVITY)
                    .withString("url","https://www.douyin.com/draft/douyin_agreement/douyin_agreement_privacy.html?id=6773901168964798477")
                    .navigation()
            }

        },0,4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        spannable1.setSpan(ForegroundColorSpan(Color.parseColor("#807F7F")), 0,4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        binding.tvProtocol.setText(spannable1)
        binding.tvProtocol.setMovementMethod(LinkMovementMethod.getInstance())
        spannable2.setSpan(object :ClickableSpan(){
            override fun onClick(p0: View) {
                ARouter.getInstance().build(RoutePath.MODULE_NATIVE_LOGIN_ACTIVITY).navigation()
            }

        },0,2,Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        spannable2.setSpan(ForegroundColorSpan(Color.parseColor("#807F7F")), 0,2, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        binding.tvLogin.setText(spannable2)
        binding.tvLogin.setMovementMethod(LinkMovementMethod.getInstance())
        binding.ivClose.setOnClickListener{
            finish()
        }
        binding.btnLogin.setOnClickListener {
            MMKV.defaultMMKV().encode("token",SPUtils.getInstance().getString("token"))
            if (GetPermissions.hasContactsPermission(this)){
                ARouter.getInstance().build(RoutePath.MAIN_ACTIVITY).navigation()
            }else{
                ARouter.getInstance().build(RoutePath.MODULE_PHONE_BOOK_ACTIVITY).navigation()
            }
        }

    }

    override fun initData() {

    }

    override fun onResume() {
        super.onResume()
        val phone = MMKV.defaultMMKV().getString("phone","")
        if (!phone.isNullOrEmpty()){
            binding.tvUsername.setText(GetPermissions.desensitizePhoneNumber(phone))
        }
    }

}